<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">
            <script>
                function title() {
                    document.getElementById('admin').className='selected';
                }
                window.onload=title;
            </script>
            <h:form id="incomeCreate" enctype="multipart/form-data">
                <h1 class="title">
                    <a href="#" class="back" onclick="history.go(-1)"/>
                    Balanço Financeiro por Intervalo de Datas
                </h1>
                <p:messages showDetail="true" autoUpdate="true"  /> 
                <div class="row">
                    <div class="twelve columns">
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Filtros</h4></legend>
                            <p:panel id="resultPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix " >

                                <p:panel id="filterPanel" header="Filtros" style="width: 93%; margin-left: 35px;"
                                         styleClass="container_24 clearfix " >
                                    <p:outputLabel for="pojoPickList" value="Convênio:" styleClass="grid_2"/>

                                    <p:pickList id="pojoPickList" value="#{managerIncome.listOperators}" 
                                                var="operator" style="width:80%; padding-left: 7px;"
                                                itemValue="#{operator}" itemLabel="#{operator.name}" 
                                                converter="operatorconverter"  
                                                showCheckbox="true" 
                                                showSourceFilter="true" showTargetFilter="true" 
                                                filterMatchMode="contains" >  

                                        <f:facet name="sourceCaption">Convênios</f:facet>  
                                        <f:facet name="targetCaption">Selecionados</f:facet>  

                                        <p:column style="width:100%;">  
                                            #{operator.ans}-#{operator.name}  
                                        </p:column>  

                                    </p:pickList>


                                    <p:outputLabel for="de" value="De: " styleClass="grid_2"/>  
                                    <p:calendar id="de" value="#{managerIncome.dateInitial}"
                                                styleClass="grid_4"  showOn="button" pattern="dd/MM/yyyy"/> 

                                    <p:outputLabel for="ate" value="Até: " styleClass="grid_2"/>  
                                    <p:calendar id="ate" value="#{managerIncome.dateFinal}" 
                                                styleClass="grid_4"  showOn="button" pattern="dd/MM/yyyy"/> 



                                    <div class="clear"/>

                                    <div class="buttonAction">
                                        <p:commandButton value="Pesquisar" icon="ui-icon-search" 
                                                         process="@form"
                                                         update="@form, :formdlg:incomePanelRemove"
                                                         actionListener="#{managerIncome.searchByDate()}" />
                                    </div>


                                </p:panel>
                                <br/>
                                <br/>
                                <br/>
                                <p:commandButton id="printBtn" value="Imprimir" icon="ui-icon-print" type="button"
                                                 styleClass="right ui-priority-primary"/>
                                <p:overlayPanel for="printBtn" style="width: 180px" >
                                    <p:commandButton 
                                        value="Relatório Simplificado" icon="ui-icon-print" immediate="true"
                                        style="width: 100%"
                                        ajax="false" onclick="this.form.target='_blank'" 
                                        action="#{managerIncome.reportUser()}"/>
                                    
                                    <p:commandButton 
                                        value="Relatório Detalhado" icon="ui-icon-print" immediate="true"
                                        ajax="false" 
                                        style="width: 100%"
                                        onclick="this.form.target='_blank'"
                                        action="#{managerIncome.reportGeral()}"/>
                                    

                                </p:overlayPanel>
                                <p:dataTable id="tableEntrada" var="in" value="#{managerIncome.incomes}" 
                                             styleClass="grid_24"
                                             rowIndexVar="rowid"
                                             rowKey="#{r.id}"
                                             rows="5" paginator="true"  
                                             paginatorPosition="bottom"
                                             emptyMessage="Nenhum registro"
                                             >  
                                    <f:facet name="header">
                                        Entradas
                                    </f:facet> 
                                    <p:column width="5">
                                        <p:rowToggler />
                                    </p:column>
                                    <p:column headerText="Pago em:" width="400" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.createAt}">
                                            <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss" />
                                        </h:outputText>
                                    </p:column> 
                                    <p:column headerText="Para:" width="400" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.systemUser.name}"/>
                                    </p:column>
                                    <p:column headerText="Procedimento" width="400" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.procedure.name}"/>
                                    </p:column>  
                                    <p:column headerText="Convênio" width="400" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.diary.operator.name}"/>
                                    </p:column>
                                    <p:column headerText="Médico" width="400" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.doctor.name}"/>
                                    </p:column>  
                                    <p:column headerText="Forma" width="400">  
                                        <h:outputText value="#{in.paymentType.name}"/>
                                    </p:column>
                                    <p:column headerText="Valor Bruto" width="400" style="text-align: right;">  
                                        <h:outputText value="#{in.amount}">
                                            <f:convertNumber pattern="#0.00" />
                                        </h:outputText>
                                    </p:column>  
                                    <p:column headerText="Valor Liquido" width="400" style="text-align: right;">  
                                        <h:outputText value="#{in.totalAmount}">
                                            <f:convertNumber pattern="#0.00" />
                                        </h:outputText>
                                        <f:facet name="footer" > 
                                            <h:outputText value="Total:" style="float: right; font-weight:bold;"/>
                                        </f:facet> 
                                    </p:column>  
                                    <p:column headerText="Valor Pago" width="400" style="text-align: right;">  
                                        <h:outputText value="#{in.amountPaid}">
                                            <f:convertNumber pattern="#0.00" />
                                        </h:outputText>
                                        <f:facet name="footer"> 
                                            <h:outputText value="#{managerIncome.total}" 
                                                          style="float: right; font-weight:bold;">
                                                <f:convertNumber pattern="#0.00" />
                                            </h:outputText>
                                        </f:facet> 
                                    </p:column>
                                    <p:rowExpansion>
                                        <p:panel style="background: none; width: 97%;" 
                                                 styleClass="container_24 clearfix " header="Detalhes" >

                                            <div class="grid_4">
                                                <p:graphicImage  width="100" styleClass="image"  alt="#{in.diary.customer.annex.id}" 
                                                                 title="#{in.diary.customer.annex.id}"
                                                                 url="#{in.diary.customer.annex.id eq null ? '':'show/?annex='}#{in.diary.customer.annex.id eq null ? 'assets/images/avatar.jpg':in.diary.customer.annex.id}" />
                                            </div>
                                            <div class="grid_20">
                                                <p:outputLabel value="Paciente: " styleClass="grid_2"/>
                                                <h:outputText value="#{in.diary.customer.name}" styleClass="grid_10"/>

                                                <p:outputLabel value="Situação: " styleClass="grid_3"/>
                                                <h:outputText value="#{in.diary.diarySituation.toString()}" styleClass="grid_5"/>


                                                <div class="clear"/>

                                                <p:outputLabel value="Observações: " styleClass="grid_5"/>
                                                <h:outputText value="#{in.diary.observation}" styleClass="grid_18"/>

                                                <div class="clear"/>

                                                <p:outputLabel value="Atendimento Realizado em:" styleClass="grid_7"/>
                                                <h:outputText value="#{in.diary.startTime}" styleClass="grid_10">
                                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"/>
                                                </h:outputText>

                                                <div class="clear"/>

                                                <p:outputLabel value="Atendente Responsável:" styleClass="grid_7"/>
                                                <h:outputText value="#{in.diary.systemUser.name}" styleClass="grid_13"/>

                                                <div class="clear"/>

                                                <h:outputText value="Agendamento Realizado em:" styleClass="grid_7"/>
                                                <h:outputText value="#{in.diary.createAt}" styleClass="grid_5">
                                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"/>
                                                </h:outputText>

                                                <p:button icon="ui-icon-search" outcome="atendimentoprocedimento.xhtml" 
                                                          value="Visualizar Atendimento" styleClass="right ui-priority-primary">
                                                    <f:param name="step" value="3"  />
                                                    <f:param name="diary" value="#{in.diary.id}" />
                                                </p:button>

                                            </div>

                                        </p:panel>
                                    </p:rowExpansion>
                                </p:dataTable> 

                                <p:commandButton 
                                    value="Imprimir" icon="ui-icon-print" immediate="true"
                                    ajax="false"  onclick="this.form.target='_blank'"
                                    styleClass="right" action="#{managerIncome.report()}"/>

                                <div class="clear"/>
                                <br/>
                                <p:dataTable id="tableOutrasReceitas" var="in" value="#{managerIncome.incomesReceitas}" 
                                             styleClass="grid_24"
                                             rowIndexVar="rowid"
                                             rowKey="#{r.id}"
                                             rows="5" paginator="true"  
                                             paginatorPosition="bottom"
                                             emptyMessage="Nenhum registro"
                                             >  
                                    <f:facet name="header">
                                        Outras Receitas (#{managerIncome.incomesReceitas.size()})
                                    </f:facet>
                                    <p:column headerText="Em" style="white-space: pre-wrap;" width="400">  
                                        <h:outputText value="#{in.createAt}">
                                            <f:convertDateTime pattern="dd/MM/yyy HH:mm:ss" />
                                        </h:outputText>
                                    </p:column>
                                    <p:column headerText="Tipo" style="white-space: pre-wrap;" width="400">  
                                        <h:outputText value="#{in.typeCredit.desc}"/>
                                    </p:column>  
                                    <p:column headerText="Recebido por" style="white-space: pre-wrap;" width="400">  
                                        <h:outputText value="#{in.systemUser.name}"/>
                                    </p:column>  

                                    <p:column headerText="Descrição" width="400">  
                                        <h:outputText value="#{in.descricao}"/>
                                        <f:facet name="footer"> 
                                            <h:outputText value="Total" 
                                                          style="float: right; font-weight:bold;"/>
                                        </f:facet> 
                                    </p:column>  
                                    <p:column headerText="Valor Pago" width="400" style="text-align: right;">  
                                        <h:outputText value="#{in.amountPaid}">
                                            <f:convertNumber pattern="#0.00" />
                                        </h:outputText>
                                        <f:facet name="footer"> 
                                            <h:outputText value="#{managerIncome.totalReceitas}" 
                                                          style="float: right; font-weight:bold;">
                                                <f:convertNumber pattern="#0.00" />
                                            </h:outputText>
                                        </f:facet> 
                                    </p:column>

                                </p:dataTable> 

                                <p:dataTable id="tableSaida" var="in" value="#{managerIncome.incomesOutput}" 
                                             styleClass="grid_24"
                                             rowIndexVar="rowid"
                                             rowKey="#{r.id}"
                                             style="margin-top: 20px;"
                                             rows="15" paginator="true"  
                                             paginatorPosition="bottom"
                                             emptyMessage="Nenhum registro"
                                             >  
                                    <f:facet name="header">
                                        Saídas
                                    </f:facet>
                                    <p:column headerText="Tipo" width="400">  
                                        <h:outputText value="#{in.typeDebt.desc}"/>
                                    </p:column>  
                                    <p:column headerText="Data Realização" width="400">  
                                        <h:outputText value="#{in.createAt}">
                                            <f:convertDateTime pattern="dd/MM/yyyy" />
                                        </h:outputText>
                                    </p:column>  
                                    <p:column headerText="Descrição" width="400" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.descricao}"/>
                                        <f:facet name="footer"> 
                                            <h:outputText value="Total" 
                                                          style="float: right; font-weight:bold;"/>
                                        </f:facet> 
                                    </p:column>  
                                    <p:column headerText="Valor Total" width="400" style="text-align: right;">  
                                        <h:outputText value="#{in.totalAmount}">
                                            <f:convertNumber pattern="#0.00" />
                                        </h:outputText>
                                        <f:facet name="footer"> 
                                            <h:outputText value="#{managerIncome.totalOutputAll}" 
                                                          style="float: right; font-weight:bold;">
                                                <f:convertNumber pattern="#0.00" />
                                            </h:outputText>
                                        </f:facet> 
                                    </p:column>
                                </p:dataTable>

                                <div class="grid_7" >
                                    <br/>

                                </div>
                                <div class="grid_7">
                                    <br/>

                                </div>

                                <div class="grid_10"  style="float: right;">
                                    <br/>
                                    <fieldset style="display: #{managerCash.verifyMaster() ? 'block':'none'}">
                                        <legend> <h5 style="margin:0 0 10px 0">Resumo</h5></legend>
                                        <h:outputText styleClass="grid_10" value=" Receitas:" style="text-align: right;font-size: 12px;"/>

                                        <h:outputText value="#{managerIncome.total.add(managerIncome.totalReceitas)}" 
                                                      styleClass="grid_13"
                                                      style="float: right; font-weight:bold; text-align: right;">
                                            <f:convertNumber pattern="R$ #0.00" />
                                        </h:outputText>

                                        <div class="clear"/>
                                        <h:outputText styleClass="grid_10" value=" Despesas:" style="text-align: right;font-size: 12px;"/>
                                        <h:outputText value="#{managerIncome.totalOutputAll}" 
                                                      styleClass="grid_13"
                                                      style="float: right; font-weight:bold; text-align: right;">
                                            <f:convertNumber pattern="R$ #0.00" />
                                        </h:outputText>

                                        <div class="clear"/>
                                        <h:outputText styleClass="grid_10" value="Total:" style="text-align: right;font-size: 12px;"/>
                                        <h:outputText value="#{(managerIncome.total.add(managerIncome.totalReceitas)).add(managerIncome.totalOutputAll)}" 
                                                      styleClass="grid_13"
                                                      style="float: right; font-weight:bold; text-align: right;">
                                            <f:convertNumber pattern="R$ #0.00" />
                                        </h:outputText>

                                        <div class="clear"/>
                                        <h:outputText styleClass="grid_12" value="Fechar caixa em :" 
                                                      rendered="#{!managerCash.existCashTodaySearch(managerIncome.dateInitial)}"
                                                      style="text-align: right;font-size: 12px;"/>

                                        <h:outputText styleClass="grid_10" value="#{managerIncome.dateInitial}" 
                                                      rendered="#{!managerCash.existCashTodaySearch(managerIncome.dateInitial)}">
                                            <f:convertDateTime pattern="dd/MM/yyyy" />
                                        </h:outputText>
                                        <div class="clear"/>
                                        <div class="buttonAction" >


                                            <p:commandButton rendered="#{!managerCash.existCashTodaySearch(managerIncome.dateInitial)}"
                                                             icon="ui-icon-close" style="width: 80%;" value="Fechar Caixa" 
                                                             onclick="dlgcash.show();" 
                                                             update="@this" process="@this"/>

                                        </div>
                                    </fieldset>
                                </div>
                            </p:panel>
                        </fieldset>
                    </div>
                </div>
            </h:form>
            <h:form id="formdlg">
                <p:dialog widgetVar="dlgcash" resizable="false" width="600" header="Deseja fechar o caixa">

                    <p:panel id="incomePanelRemove"  style="background: none; border: none;" 
                             styleClass="container_24 clearfix " >

                        <h:outputText  value="Será adicionado na conta:" styleClass="grid_23"/>  
                        <div class="clear"/>
                        <p:autoComplete id="banco"  value="#{managerCash.contaBancaria}" 
                                        styleClass="grid_23" required="true"
                                        var="item1" forceSelection="true" dropdown="true" 
                                        minQueryLength="3" itemValue="#{item1}" itemLabel="#{item1}" 
                                        panelStyle="max-height:200px;"  
                                        completeMethod="#{managerCash.autocompleteContaBancarias}" 
                                        converter="contabancariaconverter">
                        </p:autoComplete>
                        <div class="clear"/>
                        <h:outputText  value="o valor de:" styleClass="grid_23"/>  
                        <div class="clear"/>
                        <h2>
                            <h:outputText value="#{(managerIncome.total.add(managerIncome.totalReceitas))}" 
                                          style="font-weight:bold;" styleClass="grid_23">
                                <f:convertNumber pattern="R$ #0.00" />
                            </h:outputText>
                        </h2>
                        <div class="clear"/>

                        <div class="buttonAction">
                            <p:commandButton value="Sim" icon="ui-icon-disk" process="@this, :formdlg:banco"
                                             update="@all"
                                             actionListener="#{managerCash.fecharCaixaEmIntevaloDatas(managerIncome.total.add(managerIncome.totalReceitas), managerIncome.dateInitial)}" />
                            <p:commandButton value="Não" icon="ui-icon-close" process="@this"
                                             update="@form" onclick="dlgcash.hide();" />
                        </div>
                    </p:panel>
                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
